// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Splash.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;
    using System.Windows.Forms;

    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Interfaces.UI;
    using Org.OpenTrader.Framework.LiveObjects;

    #endregion

    /// <summary>
    /// The splash.
    /// </summary>
    public partial class Splash : Form, IUISplash
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Splash"/> class.
        /// </summary>
        public Splash()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Splash"/> class.
        /// </summary>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <param name="label">
        /// The label.
        /// </param>
        public Splash(TaskDelegate task, string label)
        {
            this.InitializeComponent();
            this.startStopLabel.Text = label;
            this.Task = task;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets Task.
        /// </summary>
        public TaskDelegate Task { get; private set; }

        #endregion

        #region Implemented Interfaces

        #region IUISplash

        /// <summary>
        /// The run.
        /// </summary>
        public void Run()
        {
            System.Windows.Forms.Application.Run(this);
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// The on handle created.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            Singleton<LiveObjectsRegistry>.Instance.LiveObjectsEventCB.Subscribe(this.OnLiveObjectEvent);
        }

        /// <summary>
        /// The on handle destroyed.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnHandleDestroyed(EventArgs e)
        {
            Singleton<LiveObjectsRegistry>.Instance.LiveObjectsEventCB.Unsubscribe(this.OnLiveObjectEvent);
            base.OnHandleDestroyed(e);
        }

        /// <summary>
        /// The on load.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnLoad(System.EventArgs e)
        {
            this.Task.BeginInvoke(this.OnTaskDone, new object());
        }

        /// <summary>
        /// The on live object event.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="evt">
        /// The evt.
        /// </param>
        private void OnLiveObjectEvent(object sender, LiveObjectsEventArgs evt)
        {
            this.BeginInvoke(new TaskDelegate(delegate { this.statusLabel.Text = evt.Status; }), null);
        }

        /// <summary>
        /// The on task done.
        /// </summary>
        /// <param name="r">
        /// The r.
        /// </param>
        private void OnTaskDone(IAsyncResult r)
        {
            this.BeginInvoke(new TaskDelegate(delegate { this.Close(); }), null);
        }

        #endregion
    }
}